//
//  UILabel+SJAdd.swift
//  Employee
//
//  Created by swordjoy on 2017/6/26.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

extension UILabel {
  
  static func getLabel(with font: UIFont, textColor: UIColor, textAlignment: NSTextAlignment = .left) -> UILabel {
    let label = UILabel()
    label.font = font
    label.textColor = textColor
    label.textAlignment = textAlignment
    return label
  }
  
    
   static func requiredHeight(labelText:String, width: CGFloat, font: UIFont) -> CGFloat {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: .greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.font = font
        label.text = labelText
        label.sizeToFit()
        return label.frame.height
    }
    
    static func setLabelSpace(_ label: UILabel, withValue str: String, with font: UIFont) {
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.lineBreakMode = .byCharWrapping
        paraStyle.alignment = .left
        paraStyle.lineSpacing = 4
        //设置行间距
        paraStyle.hyphenationFactor = 1.0
        paraStyle.firstLineHeadIndent = 0
        paraStyle.paragraphSpacingBefore = 0.0
        paraStyle.headIndent = 0
        paraStyle.tailIndent = 0
        //设置字间距 NSKernAttributeName:@1.5f
        let dic: [String: Any] = [NSFontAttributeName: font, NSParagraphStyleAttributeName: paraStyle, NSKernAttributeName: 1.5]
        let attributeStr = NSAttributedString(string: str, attributes: dic)
        label.attributedText = attributeStr
    }
    
    static func getSpaceLabelHeight(_ str: String, with font: UIFont, withWidth width: CGFloat) -> CGFloat {
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.lineBreakMode = .byCharWrapping
        paraStyle.alignment = .left
        paraStyle.lineSpacing = 4
        paraStyle.hyphenationFactor = 1.0
        paraStyle.firstLineHeadIndent = 0
        paraStyle.paragraphSpacingBefore = 0.0
        paraStyle.headIndent = 0
        paraStyle.tailIndent = 0
        let dic: [String: Any] = [NSFontAttributeName: font, NSParagraphStyleAttributeName: paraStyle, NSKernAttributeName: 1.5]
        let size = str.boundingRect(with: CGSize(width: width, height: 999999999), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: dic, context: nil).size
        return size.height
    }

        
    
}
